#include <stdio.h>
#include <unistd.h>
#include <string.h>

int main() {
    int fd[2];
    pid_t pid;
    char write_msg[] = "Hello, child process!";
    char read_msg[100];

    // 创建管道
    if (pipe(fd) == -1) {
        perror("pipe");
        return 1;
    }

    // 创建子进程
    pid = fork();

    if (pid < 0) {
        perror("fork");
        return 1;
    }

    if (pid > 0) { // 父进程
        close(fd[0]); // 关闭读端
        write(fd[1], write_msg, strlen(write_msg) + 1);
        close(fd[1]); // 关闭写端
    } else { // 子进程
        close(fd[1]); // 关闭写端
        read(fd[0], read_msg, sizeof(read_msg));
        printf("Received from parent: %s\n", read_msg);
        close(fd[0]); // 关闭读端
    }

    return 0;
}